<template>
  <view class="map-main">
    <web-view :src="mapLink" progress="#00FF00"></web-view>
    <!--    <view class="reset-button">    </view>-->
  </view>
</template>

<script>
import { uniRedirect } from '../../util/funcs';
import { getRouterParam } from '../../util/misc';
import { TESTING } from '../../util/test';
import { ONLINE_SERVER_ADDRESS_BASE } from '@/util/network';

export default {
  data() {
    return {
      testMap: 'online', // 可选值: localhost, online
      mapLink: '',
      houseX: '',
      houseY: '',
      word_Surrounding: '', // 可选value: '美食', '交通' 等, 用于指定搜索什么周边
      mode: '',
    };
  },
  onReady() {
    // this.setAppScalable()
  },
  onLoad() {
    // #ifdef APP-PLUS

    var pages = getCurrentPages();
    var page = pages[pages.length - 1];
    var currentWebview = page.$getAppWebview();
    setTimeout(function () {
      var web_view = currentWebview.children();
      var w = plus.nativeUI.showWaiting();
      web_view[0].addEventListener(
        'loading',
        function () {
          w.show();
        },
        false
      );

      web_view[0].addEventListener(
        'loaded',
        function () {
          w.close();
          w = null;
        },
        false
      );
    }, 200);
    // #endif
  },
  methods: {
    setAppScalable() {
      // #ifdef APP-PLUS
      //  设置可缩放
      var currentWebview = this.$mp.page.$getAppWebview(); //获取当前页面的webview对象
      let wv;
      setTimeout(() => {
        wv = currentWebview.children()[0];
        wv.setStyle({ scalable: true });
      }, 1000);
      // #endif
    },
    setMapLink() {
      let url;
      if (this.testMap === 'localhost') {
        // url = 'https://www.baidu.com'
        url = 'http://localhost:8095/map.html';
      } else if (this.testMap === 'github') {
        url = 'https://lysander686.github.io/embedMap/index.html';
      } else if (this.testMap === 'online') {
        url = ONLINE_SERVER_ADDRESS_BASE + '/embeddedMap/map.html';
      }
      url +=
        '?word=' +
        this.word_Surrounding +
        '&centerX=' +
        this.houseX +
        '&centerY=' +
        this.houseY;
      // + '&mode=' + this.mode + '/';
      console.log('url: ', url);
      // final assignment
      this.mapLink = url;
    },
    initHouseLocation() {
      let routerParams = getRouterParam();
      // console.log('传的数据:', routerParams.word_Surrounding, routerParams.lat, routerParams.lon)
      this.houseX = routerParams.build.lat;
      this.houseY = routerParams.build.lon;
      // 从上一个页面得到的周边搜索关键字
      this.word_Surrounding = routerParams.word_Surrounding;
    },
  },
  computed: {},
  mounted() {
    this.initHouseLocation();
    uni.setNavigationBarTitle({
      title: '位置周边',
      success: (res) => {
        // console.log('env:', this.testMap, this.mapLink)
        this.setMapLink();
      },
    });
  },
};
</script>

<style scoped lang="scss">
.map-main {
  .reset-button {
    height: 6vw;
    width: 6vw;
    background-color: red;
    position: fixed;
    right: 0;
    top: 50%;
    margin-top: -3vw;
    z-index: 100000;
  }
}
</style>
